package JDBC;

import java.sql.*;

/**
 * description:
 * author: 柳胜酉
 * 2022/7/7
 */
public class DBUtil {
    static Connection conn = null;
    protected static Statement sm = null;
    static PreparedStatement pstat = null;
    static ResultSet rs = null;
    static String driver = "com.mysql.jdbc.Driver";
    static String url = "jdbc:mysql://localhost:3306/user?characterEncoding=UTF-8";
    static String username = "root";
    static String password = "666666";

    static String grade = null;
    static String teacher = null;


    public static Connection getConn() {
        try {
            Class.forName(driver);
            conn = DriverManager.getConnection(url, username, password);
            sm = conn.createStatement();
            return conn;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }

    public static ResultSet getResult(String sql) {
        try {
            Class.forName(driver);
            conn = DriverManager.getConnection(url, username, password);
            sm = conn.createStatement();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        try {
            rs = sm.executeQuery(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return rs;
    }

    public static ResultSet getRs(String sql) {
        try {
            conn = getConn();
            pstat = conn.prepareStatement(sql);
            rs = pstat.executeQuery();
            return rs;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return rs;//null
    }

    public static boolean insertData(String table, String data1, String data2) {
        String sql = ("insert  into  user_info  values('" + table + "','" + data1 + "','" + data2 + "')");
        try {
            Class.forName(driver);
            conn = DriverManager.getConnection(url, username, password);
            sm = conn.createStatement();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        boolean flag = false;
        try {
            sm.executeUpdate(sql);
            flag = true;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return flag;
    }

    public static boolean insertDataOfCourse(String table, String data1, String data2) {
        if (table == "user_course_public") {

        }

        System.out.println("insert into " + table + " values('" + data1 + "','" + data2 + "')");
        String sql = ("insert into " + table + " values('" + data1 + "','" + data2 + "')");

        try {
            Class.forName(driver);
            conn = DriverManager.getConnection(url, username, password);
            sm = conn.createStatement();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        boolean flag = false;
        try {
            sm.executeUpdate(sql);
            flag = true;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return flag;
    }

    public static boolean insertDataOfCourse(String table, String data1, String data2, String data3) {
        System.out.println(data1 + data2 + data3);
        if (data2 == "数学") {
            teacher = "牛牛";
        } else if (data2 == "体育") {
            teacher = "露露";
        } else if (data2 == "英语") {
            teacher = "唔唔";
        } else if (data2 == "Java") {
            teacher = "美美";
        } else if (data2 == "C++") {
            teacher = "嘟嘟";
        } else if (data2 == "神奇宇宙") {
            teacher = "舒舒";
        } else if (data2 == "生物进化论") {
            teacher = "谷谷";
        } else if (data2 == "Python程序") {
            teacher = "酷酷";
        } else if (data2 == "茶道") {
            teacher = "虎虎";
        } else if (data2 == "安卓应用") {
            teacher = "醋醋";
        }

        if (data3 == "2021-2022第一学期"){
            grade = "大二";
        } else if (data3 == "2021-2022第二学期") {
            grade = "大二";
        } else if (data3 == "2022-2023第一学期") {
            grade = "大三";
        }

        System.out.println(data1 + data2 + data3);
        System.out.println(teacher + grade);

        System.out.println("insert into " + table + " values('" + data1 + "','" + grade + "','" + data2 + "','" + teacher + "','" + data3 + "')");
        String sql = ("insert into " + table + " values('" + data1 + "','" + grade + "','" + data2 + "','" + teacher + "','" + data3 + "')");

        try {
            Class.forName(driver);
            conn = DriverManager.getConnection(url, username, password);
            sm = conn.createStatement();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        boolean flag = false;
        try {
            sm.executeUpdate(sql);
            flag = true;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return flag;
    }

    public static boolean insertDataOfFake(String table, String data1, String data2, String data3) {

        System.out.println(data1 + data2 + data3);
        System.out.println(teacher);

        System.out.println("insert into " + table + " values('" + data1 + "','" + "大二" + "','" + data2 + "','" + data3 + "')");
        String sql = ("insert into " + table + " values('" + data1 + "','" + "大二" + "','" + data2 + "','" + data3 + "')");

        try {
            Class.forName(driver);
            conn = DriverManager.getConnection(url, username, password);
            sm = conn.createStatement();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        boolean flag = false;
        try {
            sm.executeUpdate(sql);
            flag = true;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return flag;
    }

    public static boolean insertDataCT(String table, String data1, String data2) {
        System.out.println("insert  into  user_select_t  values('" + data1 + "','" + data2 + "')");
        String sql = ("insert  into  user_select_s  values('" + data1 + "','" + data2 + "')");
        try {
            Class.forName(driver);
            conn = DriverManager.getConnection(url, username, password);
            sm = conn.createStatement();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        boolean flag = false;
        try {
            sm.executeUpdate(sql);
            flag = true;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return flag;
    }

    public static boolean insertDataT(String table, String data1, String data2) {
        String sql = ("insert  into  user_info_t  values('" + table + "','" + data1 + "','" + data2 + "')");
        try {
            Class.forName(driver);
            conn = DriverManager.getConnection(url, username, password);
            sm = conn.createStatement();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        boolean flag = false;
        try {
            sm.executeUpdate(sql);
            flag = true;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return flag;
    }

//    public static boolean insertDataT(String sclass, String sgrade, String snum, String steacher) {
//        String sql = ("insert  into  user_class  values('" + sclass + "','" + sgrade + "','" + snum + "','" + steacher + "')");
//        try {
//            Class.forName(driver);
//            conn = DriverManager.getConnection(url, username, password);
//            sm = conn.createStatement();
//        } catch (ClassNotFoundException e) {
//            e.printStackTrace();
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
//        boolean flag = false;
//        try {
//            sm.executeUpdate(sql);
//            flag = true;
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
//        return flag;
//    }

    public static boolean insertDataT(String table, String data1, String data2, String data3, String data4) {
        String sql = ("insert into " + table + " values('" + data1 + "','" + data2 + "','" + data3 + "','" + data4 + "')");
        try {
            Class.forName(driver);
            conn = DriverManager.getConnection(url, username, password);
            sm = conn.createStatement();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        boolean flag = false;
        try {
            sm.executeUpdate(sql);
            flag = true;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return flag;
    }

    public static boolean insertDataT(String table, String data1, String data2, String data3, String data4, String data5) {
        String sql = ("insert into " + table + " values('" + data1 + "','" + data2 + "','" + data3 + "','" + data4 + "','" + data5 + "')");
        try {
            Class.forName(driver);
            conn = DriverManager.getConnection(url, username, password);
            sm = conn.createStatement();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        boolean flag = false;
        try {
            sm.executeUpdate(sql);
            flag = true;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return flag;
    }

}

